package com.team06.dao;

import com.team06.pojo.Dfdl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class DfdlDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(DfdlDao.class.getName());
    private final JdbcTemplate jdbcTemplate;

    public DfdlDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Transactional
    public void insertDfdl(Dfdl dfdl) {
        // 插入主表记录
        String sql = "INSERT INTO MSG_DFOE_DFDL (" +
                "META_SNDR, META_RCVR, META_SEQN, META_DDTM, META_TYPE, META_STYP, RECD) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?)";

        jdbcTemplate.update(sql,
                dfdl.getMetaSnDR(),
                dfdl.getMetaRCVR(),
                dfdl.getMetaSEQN(),
                dfdl.getMetaDDTM(),
                dfdl.getMetaTYPE(),
                dfdl.getMetaSTYP(),
                dfdl.getRecd()
        );
        LOGGER.info("MSG_DFOE_DFDL 主表记录插入成功");

        // 获取刚插入的记录ID
        Integer msgId = jdbcTemplate.queryForObject(
                "SELECT SCOPE_IDENTITY()", Integer.class);

        if (msgId != null && dfdl.getFlights() != null) {
            // 插入航班记录
            for (Dfdl.Flight flight : dfdl.getFlights()) {
                insertFlight(flight, msgId);
            }
        }
    }

    private void insertFlight(Dfdl.Flight flight, Integer msgId) {
        String sql = "INSERT INTO DFOE_DFDL_FLIGHT (" +
                "MSG_ID, FLID, AFID, FFID, FIDE, TEST, AWCD, FLNO, FEXD, FIMD, FLIO, FLTK, FATT, PATT, " +
                "MFID, MFFI, CFTP, CFNO, STAT, ABST, ABRS, ISTA, IAST, IARS, MSTA, MABS, MABR, MIST, MIAS, " +
                "MIAR, BORT, MBOR, TBRT, MTBR, LBDT, MLBD, POKT, MPOK, APOT, DETT, DRTT, DELT, DRLT, VIP, " +
                "SFLG, PAST, FLDT, LLDT, CONT, PROX, NMCD, JMCD) " +
                "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )";

        jdbcTemplate.update(sql,
                msgId,
                flight.getFlid(),
                flight.getAfid(),
                flight.getFfid(),
                flight.getFide(),
                flight.getTest(),
                flight.getAwcd(),
                flight.getFlno(),
                flight.getFexd(),
                flight.getFimd(),
                flight.getFlio(),
                flight.getFltk(),
                flight.getFatt(),
                flight.getPatt(),
                flight.getMfid(),
                flight.getMffi(),
                flight.getCftp(),
                flight.getCfno(),
                flight.getStat(),
                flight.getAbst(),
                flight.getAbrs(),
                flight.getIsta(),
                flight.getIast(),
                flight.getIars(),
                flight.getMsta(),
                flight.getMabs(),
                flight.getMabr(),
                flight.getMist(),
                flight.getMias(),
                flight.getMiar(),
                flight.getBort(),
                flight.getMbor(),
                flight.getTbrt(),
                flight.getMtbr(),
                flight.getLbdt(),
                flight.getMlbd(),
                flight.getPokt(),
                flight.getMpok(),
                flight.getApot(),
                flight.getDett(),
                flight.getDrtt(),
                flight.getDelt(),
                flight.getDrlt(),
                flight.getVip(),
                flight.getSflg(),
                flight.getPast(),
                flight.getFldt(),
                flight.getLldt(),
                flight.getCont(),
                flight.getProx(),
                flight.getNmcd(),
                flight.getJmcd()
        );
        LOGGER.info("DFOE_DFDL_FLIGHT 航班记录插入成功");

        // 获取刚插入的航班记录ID
        Integer flightId = jdbcTemplate.queryForObject(
                "SELECT SCOPE_IDENTITY()", Integer.class);

        if (flightId != null) {
            // 插入机场记录
            if (flight.getAirports() != null) {
                for (Dfdl.Airport airport : flight.getAirports()) {
                    insertAirport(airport, flightId);
                }
            }

            // 插入行李记录
            if (flight.getBelts() != null) {
                for (Dfdl.Belt belt : flight.getBelts()) {
                    insertBelt(belt, flightId);
                }
            }
        }
    }

    private void insertAirport(Dfdl.Airport airport, Integer flightId) {
        // 修改插入语句，不包含自增列 ID
        String sql = "INSERT INTO DFOE_DFDL_AIRPORT (" +
                "FLIGHT_ID, APNO, APCD, FPTT, FETT, FRTT, FPLT, FELT, FRLT, APAT) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        jdbcTemplate.update(sql,
                flightId,
                airport.getApno(),
                airport.getApcd(),
                airport.getFptt(),
                airport.getFett(),
                airport.getFrtt(),
                airport.getFplt(),
                airport.getFelt(),
                airport.getFrlt(),
                airport.getApat()
        );
        LOGGER.info("DFOE_DFDL_AIRPORT 机场记录插入成功");
    }

    private void insertBelt(Dfdl.Belt belt, Integer flightId) {
        // 修改插入语句，不包含自增列 ID
        String sql = "INSERT INTO DFOE_DFDL_BELT (" +
                "FLIGHT_ID, BTNO, BELT_ID, CODE, BTAT, ESTR, EEND, RSTR, REND, BTSC) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        jdbcTemplate.update(sql,
                flightId,
                belt.getBtno(),
                belt.getId(),
                belt.getCode(),
                belt.getBtat(),
                belt.getEstr(),
                belt.getEend(),
                belt.getRstr(),
                belt.getRend(),
                belt.getBtsc()
        );
        LOGGER.info("DFOE_DFDL_BELT 行李记录插入成功");
    }
}